/**
 * 任务视频关联
 */
Ext.define('Module.video.TaskVideoMatch', {
	extend : 'Ext.panel.Panel',
	layout: 'border',
	border: false,
	requires: ['Sh.ux.VideoPlayer'],
	initComponent : function() {
		var me = this;
		var workStore = Ext.create('Ext.data.Store',{
			autoLoad: true,
			fields:['id','content','starttime','endtime'],
			proxy:{
				type:'ajax',
				extraParams:{task:'worksy'},
				url:'vm/getTaskList.do',
				reader:{
					root: 'datas',
					totalProperty: 'total'
				}
			}
    	});
		var xsStore = Ext.create('Ext.data.Store',{
			autoLoad: true,
			fields:['id','content','starttime','endtime'],
			proxy:{
				type:'ajax',
				extraParams:{task:'taskxs'},
				url:'vm/getTaskList.do',
				reader:{
					root: 'datas',
					totalProperty: 'total'
				}
			}
    	});
		var tab = Ext.create('Ext.tab.Panel', {
			id:me.id+'taskTab',
			region: 'west',
		    width: 740,
		    tbar: {
		    	layout:'absolute',
		    	items:[{
			    	labelWidth:60,
			    	fieldLabel:'任务内容',
			    	xtype: 'textfield',
			    	x:3,
			    	y:3
			    },{
			    	name:'startTime',
			    	xtype:'datefield',
			    	fieldLabel:'开始时间',
			    	labelWidth:60,
			    	format:'Y-m-d H:i:s',
			    	value:new Date(),
			    	x:220,
			    	y:3
			    },{
			    	name:'endTime',
			    	xtype:'datefield',
			    	fieldLabel:'至',
			    	labelWidth:25,
			    	format:'Y-m-d H:i:s',
			    	value:new Date(),
		    		x:460,
			    	y:3	
			    },{
			    	x:670,
			    	y:3,
			    	xtype:'button',
			    	iconCls: 'icon-search',
			    	text: '查询',
			    	handler: function(){
			    		var taskType = Ext.getCmp(me.id+'taskTab').getActiveTab().title;
		    			var content = this.up('tabpanel').down('field[fieldLabel=任务内容]').getValue().trim();
		    			var startTime = this.up('tabpanel').down('field[name=startTime]').getValue();
		    			var endTime = this.up('tabpanel').down('field[name=endTime]').getValue();
		    				if('工作任务' == taskType){
		    					workStore.load({params:{
		    						task:'worksy',
		    						content:content,
		    						startTime:startTime,
		    						endTime:endTime
		    					}});
		    				}else{
		    					xsStore.load({params:{
		    						task:'taskxs',
		    						content:content,
		    						startTime:startTime,
		    						endTime:endTime
		    					}});
		    				}
			    	}
			    }],
			    listeners:{
			    	afterrender:function(){
			    		this.up('tabpanel').down('field[fieldLabel=任务内容]').setValue();
		    			this.up('tabpanel').down('field[name=startTime]').setValue();
		    			this.up('tabpanel').down('field[name=endTime]').setValue();
			    	}
			    }
		    },
		    items: [{
		        title: '工作任务',
		        taskType:1,
	        	layout:'fit',
	        	xtype:'grid',
	        	bbar: Ext.create('Ext.PagingToolbar', {
		            store: workStore,
		            displayInfo: true,
		            displayMsg: 'Displaying topics {0} - {1} of {2}',
		            emptyMsg: "No topics to display"
		        }),
	        	columns:[
	        	    {dataIndex:'content',width:260,text:'任务内容'},
	        		{dataIndex:'starttime',width:150,text:'开始时间',renderer:formatDate},
	        		{dataIndex:'endtime',width:150,text:'结束时间',renderer:formatDate}
	        	],
	        	store:workStore,
			    listeners:{
			    	/*itemdblclick:function(s,rec,item){
			    		me.upwin(rec,tab.getActiveTab().taskType,false);
			    	},*/
	        		cellclick:function(s,td,index,rec){
	        			Ext.getCmp(me.id+'vgrid').store.load({
	        				params:{
	        					taskId: rec.raw.id,
	        					status:1,
	        					taskType:1//1-表示工作任务
	        				}
	        			});
	        		}
			    }
	        
		    }, {
		        title: '巡视任务',
		        taskType:2,
		        layout:'fit',
	        	xtype:'grid',
	        	bbar: Ext.create('Ext.PagingToolbar', {
		            store: xsStore,
		            displayInfo: true,
		            displayMsg: 'Displaying topics {0} - {1} of {2}',
		            emptyMsg: "No topics to display"
		        }),
		        columns:[
			        	    {dataIndex:'content',width:260,text:'任务内容'},
			        		{dataIndex:'starttime',width:150,text:'开始时间',renderer:formatDate},
			        		{dataIndex:'endtime',width:150,text:'结束时间',renderer:formatDate}
			    ],
	        	store:xsStore,
			    listeners:{
			    	/*itemdblclick:function(s,rec,item){
			    		me.twin(rec);
			    	},*/
	        		cellclick:function(s,td,index,rec){
	        			Ext.getCmp(me.id+'vgrid').store.load({
	        				params:{
	        					taskId: rec.raw.id,
	        					status:1,
	        					taskType:2//1-表示巡视任务
	        				}
	        			});
	        		}
			    }
	        
		    }]
		});
		
		var _columns = [{
			dataIndex:'bz_name',
			width:110,
			text:'班组名称'
		},{
			dataIndex:'rolename',
			width:100,
			text:'角色'
		},{
			dataIndex:'zygc_name',
			width:100,
			text:'作业过程'
				}/*,{
					text:'是否关联',
					dataIndex:'status',
					width:60,
					renderer:function(v){
						return v == 1?'已关联':'未关联';
					}
				}*/,{
			dataIndex:'name',
			width:200,
			text:'视频名称'
		},{
			dataIndex:'syntime',
			width:130,
			text:'同步时间',
			renderer: formatDate
		},{
			dataIndex:'videotime',
			width:80,
			text:'视频时长',
			renderer:function(v){
				var t = v/1000;
			    var m = Math.floor(t / 60);
			    m < 10 && (m = '0' + m);
			    return m + ":" + (t % 60 / 100).toFixed(2).slice(-2);
			}
		}/*,{
			dataIndex:'rawpath',
			width:200,
			text:'原始路径'
		},{
			dataIndex:'synpath',
			width:200,
			text:'同步路径'
		}*/];
		
		var _fields = ['id','name','starttime','endtime','videotime','syntime','rawpath','synpath',
		               'bz_name','rolename','zygc_name','status'];
		
		var grid = Ext.create('Ext.grid.Panel',{
			id:me.id+'vgrid',
			region: 'center',
			title:'关联视频',
			tbar:[{
				text:'添加视频',
				iconCls:'icon-add',
				handler:function(){
//					var rec = this.up('tabpanel').down('grid').getSelectionModel().getSelection()[0];
					var rec =  Ext.getCmp(me.id+'taskTab').getActiveTab().getSelectionModel().getSelection()[0];
					var taskType = Ext.getCmp(me.id+'taskTab').getActiveTab().taskType;
					if(rec == undefined){
						Ext.Msg.alert('提示','请选择关联任务。');
						return;
					}
					me.addwin(rec,taskType);
				}
			},{
				text:'修改视频',
				iconCls:'icon-edit',
				handler:function(){
					var acttab =  Ext.getCmp(me.id+'taskTab').getActiveTab();
					//var tabGrid = this.up('tabpanel').down('grid').getSelectionModel().getSelection()[0];
//					var rec = grid.getSelectionModel().getSelection()[0];
					var rec = acttab.getSelectionModel().getSelection()[0];
					var d = {
							rec:rec,
							acttab:acttab
					};
					if(rec == undefined){
						Ext.Msg.alert('提示','请选择任务。');
						return;	
					}
					if(Ext.getCmp(me.id+'vgrid').getSelectionModel().getSelection()[0] == undefined){
						Ext.Msg.alert('提示','请选择已关联的视频。');
						return;
					}
//					me.twin(d,true);
					me.upwin(rec, acttab.taskType, true);
				}
			},{
				text:'删除视频',
				iconCls:'icon-delete',
				handler:function(){
					var rec = grid.getSelectionModel().getSelection()[0];
					if(rec && rec.raw.match == 0){
						Ext.Msg.alert('提示','请选择已关联的视频。');
						return;
					}
					me.delwin(rec,grid);
				}
			},{
				text:'播放',
				iconCls:'icon-play',
				handler:function(){
					var rec = Ext.getCmp(me.id+'vgrid').getSelectionModel().getSelection()[0];
					if(rec == undefined){
						Ext.Msg.alert("提示","请选择要播放的视频。");
						return ;
					}
					me.playWin(rec);
				}
			}],
			columns:_columns,
        	store:Ext.create('Ext.data.Store',{
//				autoLoad: true,
				fields:_fields,
				proxy:{
					type:'ajax',
//					extraParams:{status:1},
					url:'vm/getSynPath.do'
				}
        	})
			
		});
		
		me.items = [tab,grid];
		me.defaults = {split: true};
		me.callParent();
	},
	getVideo:function(videoId){
		var me = this;
		var vitems = [];
		Ext.Ajax.request({
			url:'vm/getSynPath.do',
			async:false,
			params:{
				id:videoId
			},
			success:function(response){
				var text = Ext.decode(response.responseText);
//				console.log(text);
				if(text){
					for(var i=0;i<text.length;i++){
						vitems.push({
							xtype: 'player',
							title: text[i].name,
							src: basePath+text[i].synpath
						});
					}
				}
			}
		});
		return vitems;
	},
	delwin:function(rec,grid){
		var me = this;
		Ext.Msg.confirm('提示', '你确定要删除当前数据吗？',function(b){
			if('yes' == b){
				Ext.Ajax.request({
				    url: 'vm/deleteVSyn.do',
				    params: {
				        id: rec.raw.id
				    },
				    success: function(response,action){
				    	if(response.responseText == 1){
				    		Ext.Msg.alert('提示', '删除成功！');
				    		grid.store.reload();
				    	}else{
				    		Ext.Msg.alert('提示', '删除失败！');
				    	}
				    }
				});
			}
		});
	},
	addwin:function(rec,taskType){
		var me = this;
		var _awin = Ext.create("Ext.window.Window",{
			layout:'border',
			width:600,
			height:500,
			modal:true,
			iconCls:'icon-add',
			title:'添加任务视频',
			buttons:[{
				text:'保存',
				handler:function(){
					var thiswin = this.up("window");
					var taskId = rec.raw.id;
					var bzId = thiswin.down('combobox[name=bzname]').getValue();
					var roleId = thiswin.down('combobox[name=bzjs]').getValue();
					var stageId = thiswin.down('combobox[name=gzjd]').getValue();
					var videoId = thiswin.down('grid').getSelectionModel().getSelection()[0];
					if(bzId == undefined){
						Ext.Msg.alert('提示',"请选择工作班组");return;
					}
					if(roleId == undefined){
						Ext.Msg.alert('提示',"请选择班组角色");return;
					}
					if(stageId == undefined){
						Ext.Msg.alert('提示',"请选工作阶段");return;
					}
					if(videoId == undefined){
						Ext.Msg.alert('提示',"请选择要关联的视频");return;
					}
					Ext.Ajax.request({
						url:'vm/addVideoManage.do',
						params:{
							bz_id:bzId,
							role_id:roleId,
							stage_id:stageId,
							video_id:videoId.raw.id,
							task_id:taskId,
							taskType:taskType
						},
						success:function(resp){
							if(resp.responseText == 1){
								Ext.Msg.alert("提示","关联视频成功");
								//var tabGrid = this.up('tabpanel').down('grid').getSelectionModel().getSelection()[0];
//								thiswin.down('grid').store.reload();
								Ext.getCmp(me.id+'vgrid').store.reload();
								_awin.close();
							}else if(resp.responseText == 0){
								Ext.Msg.alert("提示","此视频已关联");
							}
							else{
								Ext.Msg.alert("提示","关联视频失败");
							}
						}
					});
				}
			},{
				text:'取消',
				handler:function(){
					this.up("window").close();
				}
			}],
			items:[{
				region: 'north',
				xtype:'form',
				bodyPadding: 10,
				border: false,
				defaults: {anchor: '100%',allowBlank: false},
				items:[{
					xtype:'combobox',
					name:'bzname',
					fieldLabel: '工作班组',
					readOlay:true,
					displayField: 'name',
					valueField: 'id',
					queryMode: 'local',
					store: Ext.create('Ext.data.Store',{
						autoLoad: true,
						fields: ['id','name'],
						proxy: {
							type: 'ajax',
							extraParams:{bzId:rec.raw.bzid},
							url: 'worksy/getBz.do'
						}
					})
				},{
					xtype:'combobox',
					name:'bzjs',
					fieldLabel: '班组角色',
					readOlay:true,
					displayField: 'name',
					valueField: 'id',
					queryMode: 'local',
					store: Ext.create('Ext.data.Store',{
						fields: ['id','name'],
						proxy: {
							type: 'ajax',
							url: 'vm/getBzRole.do'
						}
					}),
					listeners:{
						afterrender:function(c){
							var bzname = c.up('form').down('combobox[name=bzname]');
							c.addManagedListener(bzname,'select',function(){
								c.setValue();
								c.store.load({params:{
									bzId: bzname.getSubmitValue()
								}});
							});
						}
					}
				},{
					xtype:'combobox',
					name:'gzjd',
					fieldLabel: '工作阶段',
					readOlay:true,
					displayField: 'name',
					valueField: 'id',
					queryMode: 'local',
					store: Ext.create('Ext.data.Store',{
						//autoLoad: true,
						fields: ['id','name'],
						proxy: {
							type: 'ajax',
							extraParams:{rwid: rec.raw.id},
							url: 'vm/getGc.do'
						}
					}),
					listeners:{
						afterrender:function(c){
							var _bzjs = c.up('form').down('combobox[name=bzjs]');
							c.addManagedListener(_bzjs,'select',function(){
								c.setValue();
								c.store.load({params:{
									bzJc: _bzjs.getSubmitValue()
								}});
							});
						}
					}
				}]
			},{
				region: 'center',
				xtype:'grid',
				selModel: Ext.create('Ext.selection.CheckboxModel',{
					mode:'SINGLE'
				}),
				tbar:[{
					labelWidth:65,
					fieldLabel: '视频名称',
					xtype:'textfield'
				},{
					fieldLabel:'同步时间',
					xtype: 'datefield',
					labelWidth: 65,
					width: 170,
					format:'Y-m-d',
					value:new Date(),
					submitFormat: 'Y-m-d'
				},{
					fieldLabel:'至',
					xtype: 'datefield',
					labelWidth: 20,
					width: 125,
					value: new Date(),
					format:'Y-m-d',
					submitFormat: 'Y-m-d'
				},{
					text:'查询',
					width: 55,
					iconCls: 'icon-search',
					handler:function(){
						var xgrid = this.up('window').down('grid');
						xgrid.store.proxy.extraParams = {
								videoName: xgrid.down('field[fieldLabel=视频名称]').getSubmitValue(),
								synStartTime: xgrid.down('field[fieldLabel=同步时间]').getSubmitValue(),
								synEndTime: xgrid.down('field[fieldLabel=至]').getSubmitValue(),
								/*status:0,*/
								taskType:taskType
						};
						xgrid.store.load();
					}
				}],
				columns:[{
					text:'视频名称',dataIndex:'name',width:200
				},{
					text:'原始路径',dataIndex:'rawpath',width:200
				},{
					text:'同步时间',dataIndex:'syntime',width:130,renderer: formatDate
				}], 
	        	store:Ext.create('Ext.data.Store',{
					autoLoad: true,
					fields:['name','rawpath','syntime'],
					proxy:{
						type:'ajax',
						extraParams:{/*status:0,*/taskType:taskType},
						url:'vm/getSynPath.do'
					}
	        	})
			}]
		}).show();
	},
	playWin:function(rec){
		var me = this;
		Ext.create("Ext.window.Window",{
			layout:'fit',
			width:1000,
			height:600,
			modal:true,
			iconCls:'icon-play',
			title:'视频播放',
			items: [{
				xtype: 'player',
				autoPlay: true,
				vdata: {id:rec.data.id},
				src: basePath+rec.raw.synpath,
				border: false
			}]
		}).show();
	},
	upwin:function(rec,taskType,flag){
		var me = this;
		var upwindow = Ext.create("Ext.window.Window",{
			layout:'border',
			width:600,
			height:500,
			iconCls:'icon-cog_edit',
			modal:true,
			title:'视频关联',
			buttons:[{
				text:'保存',
				handler:function(){
					var thiswin = this.up("window");
					var taskId = rec.raw.id;
					//班组ID
					var bzId = thiswin.down('combobox[name=bzname]').getValue();
					//班组角色ID
					var roleId = thiswin.down('combobox[name=bzjs]').getValue();
					//作业阶段ID
					var stageId = thiswin.down('combobox[name=gzjd]').getValue();
					//同步视频ID
					var videoId = thiswin.down('grid').getSelectionModel().getSelection()[0];
					if(bzId == undefined){
						Ext.Msg.alert('提示',"请选择工作班组");return;
					}
					if(roleId == undefined){
						Ext.Msg.alert('提示',"请选择班组角色");return;
					}
					if(stageId == undefined){
						Ext.Msg.alert('提示',"请选工作阶段");return;
					}
					if(videoId == undefined){
						Ext.Msg.alert('提示',"请选择要关联的视频");return;
					}
//					console.log(Ext.getCmp(me.id+'vgrid').getSelectionModel().getSelection()[0]);
					Ext.Ajax.request({
						url:'vm/addVideoManage.do',
						params:{
							bz_id:bzId,
							video_id:videoId.raw.id,
							stage_id:stageId,
							task_id:taskId,
							role_id:roleId,
							mark:flag?'update':'',//判断是否为修改数据标识
							delId:Ext.getCmp(me.id+'vgrid').getSelectionModel().getSelection()[0].data.id //已经关联的视频id,传入后天删除相关的数据
						},
						success:function(resp){
							//var d =Ext.decode(resp.responseText);
							if(resp.responseText == 1){
								/*Ext.getCmp(me.id+'vplay').store.load({
									params:{
			        					status:0,
			        					bzId:bzid
			        				}
								});*/
								Ext.getCmp(me.id+'vgrid').store.reload();
								upwindow.close();
								Ext.Msg.alert('提示', '关联成功！');
							}else if(resp.responseText == 0){
								Ext.Msg.alert('提示', '此视频已关联，请选择其他视频！');
							}else{
								Ext.Msg.alert('提示', '关联失败！');
							}
						}
					});
				
				}
			},{
				text:'关闭',
				handler:function(){
					upwindow.close();
				}
			}],
			items:[{
				region: 'north',
				xtype:'form',
				bodyPadding: 10,
				border: false,
				defaults: {anchor: '100%',allowBlank: false},
				items:[{
					xtype:'combobox',
					name:'bzname',
					fieldLabel: '工作班组',
					readOlay:true,
					displayField: 'name',
					valueField: 'id',
					queryMode: 'local',
					store: Ext.create('Ext.data.Store',{
						autoLoad: true,
						fields: ['id','name'],
						proxy: {
							type: 'ajax',
							extraParams:{bzId:rec.raw.bzid},
							url: 'worksy/getBz.do'
						}
					})
				},{
					xtype:'combobox',
					name:'bzjs',
					fieldLabel: '班组角色',
					readOlay:true,
					displayField: 'name',
					valueField: 'id',
					queryMode: 'local',
					store: Ext.create('Ext.data.Store',{
						fields: ['id','name'],
						proxy: {
							type: 'ajax',
							url: 'vm/getBzRole.do'
						}
					}),
					listeners:{
						afterrender:function(c){
							var bzname = c.up('form').down('combobox[name=bzname]');
							c.addManagedListener(bzname,'select',function(){
								c.setValue();
								c.store.load({params:{
									bzId: bzname.getSubmitValue()
								}});
							});
						}
					}
				},{
					xtype:'combobox',
					name:'gzjd',
					fieldLabel: '工作阶段',
					readOlay:true,
					displayField: 'name',
					valueField: 'id',
					queryMode: 'local',
					store: Ext.create('Ext.data.Store',{
						//autoLoad: true,
						fields: ['id','name'],
						proxy: {
							type: 'ajax',
							extraParams:{rwid: rec.raw.id},
							url: 'vm/getGc.do'
						}
					}),
					listeners:{
						afterrender:function(c){
							var _bzjs = c.up('form').down('combobox[name=bzjs]');
							c.addManagedListener(_bzjs,'select',function(){
								c.setValue();
								c.store.load({params:{
									bzJc: _bzjs.getSubmitValue()
								}});
							});
						}
					}
				}]
			},{
				region: 'center',
				xtype:'grid',
				selModel: Ext.create('Ext.selection.CheckboxModel',{
					mode:'SINGLE'
				}),
				tbar:[{
					labelWidth:65,
					fieldLabel: '视频名称',
					xtype:'textfield'
				},{
					fieldLabel:'同步时间',
					xtype: 'datefield',
					labelWidth: 65,
					width: 170,
					format:'Y-m-d',
					value:new Date(),
					submitFormat: 'Y-m-d'
				},{
					fieldLabel:'至',
					xtype: 'datefield',
					labelWidth: 20,
					width: 125,
					value: new Date(),
					format:'Y-m-d',
					submitFormat: 'Y-m-d'
				},{
					text:'查询',
					width: 55,
					iconCls: 'icon-search',
					handler:function(){
						var xgrid = this.up('window').down('grid');
						xgrid.store.proxy.extraParams = {
								videoName: xgrid.down('field[fieldLabel=视频名称]').getSubmitValue(),
								synStartTime: xgrid.down('field[fieldLabel=同步时间]').getSubmitValue(),
								synEndTime: xgrid.down('field[fieldLabel=至]').getSubmitValue(),
								status:0,
								taskType:taskType
						};
						xgrid.store.load();
					}
				}],
				columns:[{
					text:'视频名称',dataIndex:'name',width:200
				},{
					text:'原始路径',dataIndex:'rawpath',width:200
				},{
					text:'同步时间',dataIndex:'syntime',width:130,renderer: formatDate
				}], 
	        	store:Ext.create('Ext.data.Store',{
					autoLoad: true,
					fields:['name','rawpath','syntime'],
					proxy:{
						type:'ajax',
						extraParams:{/*status:0,*/taskType:taskType},
						url:'vm/getSynPath.do'
					}
	        	})
			}]
		}).show();
	}
	
});